from django.shortcuts import render
from django.http import JsonResponse, HttpResponse
from django.db.models import Q
from django.conf import settings
from user.models import User, VSession, WarZone, Company, IPSystem
from user.dataCheck import DataCheck
from log.models import UserLog
from storehouse.models import Storehouse
import pdb, os, time, requests, base64, json
import datetime
import logging, random
from conf.sysConfig import config
from pprint import pprint

# from io import BytesIO

tmpDir = os.path.join(config["dataDir"], config["tmpDir"])
logger_request = logging.getLogger("django.request")
userSystem = config["userSystem"]
isUserSystem = config["isUserSystem"]
redirect_url = config["redirect_url"]
de_redirect_url = config["de_redirect_url"]
user_redirect_url = config["user_redirect_url"]
userPort = config["userPort"]
userToken = config["userToken"]
userInfo = config["userInfo"]

datacheck = DataCheck()


def supervisorUserList(request, *args, **kwargs):
    form = request.checkForm
    requestParam = form.cleaned_data

    q1 = Q()
    q2 = Q()
    filterDict = {}
    if requestParam["search"] != "":
        q1 = Q(username__contains=requestParam["search"])
        q2 = Q(name__icontains=requestParam["search"])

    if requestParam["userType"] != "":
        filterDict["userType"] = requestParam['userType']

    userList = User.objects.filter(q1 | q2).filter(**filterDict)
    totalNum = len(userList)
    result = userList[min(totalNum, (requestParam['page'] - 1) * requestParam['maxItem']): min(totalNum,
                                                                                               requestParam[
                                                                                                   'page'] *
                                                                                               requestParam[
                                                                                                   'maxItem'])]
    userInfo = []
    user = datacheck.getUser(requestParam["token"])
    for item in result:
        if item.id == user.id:
            own = "1"
        else:
            own = "0"
        itemInfo = {
            "username": item.username,
            "name": item.name,
            "userType": item.userType,
            "warZone": item.company.warZone.name,
            "company": item.company.name,
            "createTime": item.createTime.strftime('%Y-%m-%d %H:%M:%S'),
            "cardId": item.cardId,
            "id": item.id,
            "isOwn": own
        }
        userInfo.append(itemInfo)

    data = {
        "code": 0,
        "msg": "success",
        "userInfo": userInfo,
        "totalNum": totalNum,
    }

    return JsonResponse(data)


def supervisorEdit(request, *args, **kwargs):
    form = request.checkForm
    requestParam = form.cleaned_data

    editUser = User.objects.filter(pk=requestParam["userId"]).first()

    editUser.userType = requestParam["userType"]
    editUser.save()

    data = {
        "code": 0,
        "msg": "success",
    }

    return JsonResponse(data)
